<?php

use \Doraemon\pockets\constInterface\ApiCode;
use Doraemon\tools\Log;


class ErrorController extends Yaf_Controller_Abstract {

	//从2.1开始, errorAction支持直接通过参数获取异常
	public function errorAction($exception) {
        $this->setError($exception);
	}

    protected function setError($e = null)
    {
        $reportCode = uniqid();
        $view = $view = [
            "code" => ApiCode::API_CODE_EXCEPTION,
            "msg" => \Doraemon\pockets\action\ApiAction::API_CODE_EXCEPTION_TEXT . "反馈码：" . $reportCode
        ];
        $logMethod = 'api_error-' . $view['code'] . '_error';
        Log::$logMethod(implode(Log::DEFAULE_LOG_SEPARATOR,
            [
                $e ? $e->getLine() : "",
                $reportCode,
            ]), [
            $e ?: "",
            $this->getRequest(),
            $this->getRequest()->getQuery(),
            $this->getRequest()->getPost()
        ]);
        $this->getResponse()->setBody(json_encode($view, JSON_UNESCAPED_UNICODE));
    }


}